<?php

namespace apiend\models;

use Yii;
use yii\filters\RateLimitInterface;
use common\helpers\Url;
use common\helpers\ColorHelper;

/**
 * 账号信息
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class User extends \common\models\BaseUser implements RateLimitInterface {

    public $token;

    /**
     * 返回在单位时间内允许的请求的最大数目，例如，[10, 60] 表示在60秒内最多请求10次。
     * @return type
     */
    public function getAllowance() {
        return Yii::$app->cache->get($this->id . '_allowance');
    }

    /**
     * 返回更新时间
     */
    public function getAllowanceUpdatedAt() {
        return Yii::$app->cache->get($this->id . '_allowance_updated_at');
    }

    /**
     * 返回在单位时间内允许的请求的最大数目，例如，[10, 60] 表示在60秒内最多请求10次。
     * @return type
     */
    public function getRateLimit($request, $action) {
        return [20, 1];
    }

    /**
     * 返回剩余的允许的请求数
     * @return type
     */
    public function loadAllowance($request, $action) {
        return [$this->allowance, $this->allowanceUpdatedAt];
    }

    /**
     * 保存请求时的UNIX时间戳
     * @return type
     */
    public function saveAllowance($request, $action, $allowance, $timestamp) {
        Yii::$app->cache->set($this->id . '_allowance', $allowance);
        Yii::$app->cache->set($this->id . '_allowance_updated_at', $timestamp);
    }


    /**
     * 获取会员信息
     * @return type
     */
    public function getTheme() {
        $member = $this->avalidMember;
        if ($member) {
            $levelIcon = $member->level ? $member->level->levelIcon : '';
            $avatarFrame = '';
            $nameColor = ColorHelper::format('#ff6600');
            if ($member->type) {
                $avatarFrame = $member->type->avatarFrame;
                $nameColor = $member->type->nameColor;
            }
            return [
                'level_icon' => $levelIcon,
                'avatar_frame' => $avatarFrame,
                'username_color' => $nameColor,
            ];
        }
        return null;
    }

    public function fields() {
        return [
            'id',
            'nickname',
            'mobile' => function() {
                if ($this->mobile_hash) {
                    return $this->decryptMobile();
                }
                return '';
            },
            'avatar' => function() {
                return Url::attach($this->avatar);
            },
            'email' => function() {
                return (string) $this->email;
            },
            'location' => function() {
                return $this->meta ? $this->meta->location : '';
            },
            'gender' => function() {
                return $this->meta ? $this->meta->gender : '';
            },
            'signature' => function() {
                return $this->meta ? $this->meta->signature : '';
            },
            'birthday' => function() {
                return $this->meta ? $this->meta->birthday : '';
            },
            'register_time' => 'created_at',
            'rongyun_token' => 'rongCloudToken',
            'collects',
            'experiences',
            'discount',
            'theme'
        ];
    }

}
